/** @brief Define commonly used classes or functions.
 * 
 *  util.cpp
 *  kangmodb
 *
 *  Created by 강모 김 on 11. 4. 25..
 *  Copyright 2011 강모소프트. All rights reserved.
 *
 */
#include "kdInfra.h"
#include "util.h"

namespace util {
	int string_compare(const char * string1, const int string1_len, const char * string2, const int string2_len )
	{
		KD_ASSERT( string1_len >= 0 );
		KD_ASSERT( string2_len >= 0 );

		// Treat the string having MAX value if it points to NULL.
		if ( string1 == NULL & string2 == NULL ) // MAX == MAX 
			return 0;
		if ( string1 == NULL) // MAX > any
			return 1;
		if ( string2 == NULL) // any < max
			return -1;

		int cmpLength = MIN(string1_len, string2_len);
		int cmpResult = strncmp(string1, string2, cmpLength );
		
		// Same until the length of the shorter key? 
		// ex> strncmp( 'ABC', 'ABC00', 3)
		if ( cmpResult == 0 ) {
			// longer key is greater.
			// ex> ABC < ABC00
			cmpResult = (int) (string1_len - string2_len);
		}
		
		return cmpResult;
	}
}
